package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.mapper.MemberReservationMapper;
import com.zzyl.nursing.service.MemberReservationService;
import com.zzyl.nursing.vo.CountByTimeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class MemberReservationServiceImpl extends ServiceImpl<MemberReservationMapper, Reservation> implements MemberReservationService {

    @Autowired
    private MemberReservationMapper memberReservationMapper;
    @Override
    public Integer getCancelledReservationCount(Long userId) {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        Integer count = memberReservationMapper.getCancelledReservationCount(userId,startTime, endTime);
        return count;
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        reservation.setStatus(0);
        return save(reservation) ? 1 : 0;
    }

    @Override
    public List<Reservation> list(Reservation reservation) {
        List<Reservation> reservations = memberReservationMapper.selectReservationList(reservation);
        return reservations;
    }

    @Override
    public void cancel(Long id) {
        boolean update = this.update(Wrappers.<Reservation>lambdaUpdate()
                .eq(Reservation::getId, id)
                .set(Reservation::getStatus,2 ));
    }

    @Override
    public List<CountByTimeVo> countByTime(Long time) {
        Date date = new Date(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        List<CountByTimeVo> countByTimeVos = memberReservationMapper.countByTime(format);
        return countByTimeVos;
    }
}
